package todo_api

import (
	"github.com/daqing/airway/app/models"
	"github.com/daqing/airway/lib/db"
	"github.com/daqing/airway/lib/resp"
	"github.com/daqing/airway/lib/sql"
	"github.com/gin-gonic/gin"
)

type UpdateParams struct {
	Id    int    `json:"id"`
	Title string `json:"title"`
}

func UpdateAction(c *gin.Context) {
	var p UpdateParams

	if err := c.BindJSON(&p); err != nil {
		resp.Error(c, err)
		return
	}

	err := db.UpdateById[models.TodoItem](sql.IdType(p.Id), sql.H{"title": p.Title})
	if err != nil {
		resp.Error(c, err)
		return
	}

	resp.OK(c, gin.H{"title": p.Title})
}
